package com.scau.beyondboy.controller;
import com.scau.beyondboy.model.UserEntity;
import com.scau.beyondboy.services.UserServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
/**
 * Author:beyondboy
 * Gmail:xuguoli.scau@gmail.com
 * Date: 2015/12/11
 * Time: 0:55
 */
@Controller
@RequestMapping(value = "/user")
public class UserController
{
    @Autowired
    UserServices userServices;
    @RequestMapping(value = "/loginhandle",method = RequestMethod.POST)
    public String loginhandle(ModelMap modelMap,@RequestParam(value ="username")String username,@RequestParam(value="password")String password)
    {
        if(StringUtils.isEmpty(username)||StringUtils.isEmpty(password))
        {
            modelMap.addAttribute("error","用户名和密码都不能为空");
            return "loginfail";
        }
        else if(userServices.checkLogin(username,password))
        {
            return "redirect:/book/show";
        }
        modelMap.addAttribute("error","不存在该用户");
        return "loginfail";
    }
    @RequestMapping(value = "/login")
    public String login()
    {
        return "login";
    }
    @RequestMapping(value ="/register")
    public String register()
    {
        return "adduser";
    }
    @RequestMapping(value="/adduser")
    public String addUser(UserEntity userEntity)
    {
        System.out.println(userEntity.getUsername()+"         "+userEntity.getPassword());
        userServices.addUser(userEntity.getUsername(),userEntity.getPassword());
        return "hello";
    }
}
